package com.yxhl.zoume.core.car.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.custom.RevealLayout;
import com.yxhl.zoume.core.func.map.ui.event.FinishLocationSelectEvent;
import com.yxhl.zoume.core.func.map.ui.event.SelectedPOIEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.SoftInputUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sugarya on 16/4/14.
 */
public class CarPOISearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "SearchRecyclerAda";

    private Context mContext;
    private List<PoiItem> mPoiItems;
    private LinearLayoutManager mLayoutManager;


    public CarPOISearchAdapter(Context context, List<PoiItem> mPoiItems, LinearLayoutManager mLayoutManager) {
        this.mPoiItems = mPoiItems;
        this.mContext = context;
        this.mLayoutManager = mLayoutManager;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_car_location_search, parent, false);
        return new SearchViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SearchViewHolder) {
            SearchViewHolder searchViewHolder = (SearchViewHolder) holder;
            PoiItem poiItem = mPoiItems.get(position);
            if (poiItem != null) {
                searchViewHolder.setIcon(position);
                searchViewHolder.setTitle(poiItem.getTitle(), position);
                searchViewHolder.setDescribe(poiItem.getProvinceName() + poiItem.getCityName() + poiItem.getAdName());
            }
        }
    }

    @Override
    public int getItemCount() {
        return mPoiItems.size();
    }

    public void notifyData(List<PoiItem> poiItems, PoiItem locationPoiItem) {
        if (poiItems != null) {
            mPoiItems.clear();
            if (locationPoiItem != null && poiItems.size() > 0) {
                mPoiItems.add(locationPoiItem);
            }
            mPoiItems.addAll(poiItems);

            notifyDataSetChanged();
        }
    }

    /**
     * 清空列表
     */
    public void notifyEmpty() {
        mPoiItems.clear();
        notifyDataSetChanged();
    }

    class SearchViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.reveal_special_car_station_search_root)
        RevealLayout revealLayout;

        @BindView(R.id.iv_item_special_car_search_poi)
        ImageView searchPOIIv;
        @BindView(R.id.item_special_car_search_poi_title)
        TextView titleTv;
        @BindView(R.id.item_special_car_search_poi_describe)
        TextView describeTv;

        public SearchViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.reveal_special_car_station_search_root)
        public void onItemClick(View view) {
            int position = mLayoutManager.getPosition(view);
            if (position >= 0 && position < mPoiItems.size()) {
                //关闭输入法
                SoftInputUtils.closeInputKeyboard((Activity) mContext);

                PoiItem poiItem = mPoiItems.get(position);
                String title = poiItem.getTitle();
                String adCode = poiItem.getAdCode();
                LOG.e(TAG, "onPopupItem title=" + title);
                LOG.e(TAG, "onPopupItem adCode=" + adCode);

                if (!TextUtils.isEmpty(adCode) && !TextUtils.isEmpty(title)) {
                    RxBus.getInstance().send(new SelectedPOIEvent(poiItem));
                    RxBus.getInstance().send(new FinishLocationSelectEvent());
                } else {
                    PromptUtils.showShort(mContext, mContext.getString(R.string.special_car_poi_empty));
                }
            }
        }

        void setIcon(int position) {
            switch (position) {
                case 0:
                    searchPOIIv.setImageResource(R.drawable.ic_dangqian_weizhi);
                    break;
                default:
                    searchPOIIv.setImageResource(R.drawable.ic_search_weizhi);
            }
        }

        void setTitle(String title, int position) {
            if (!TextUtils.isEmpty(title)) {
                switch (position) {
                    case 0:
                        titleTv.setTypeface(Typeface.DEFAULT);
                        titleTv.setText(title);
                        break;
                    default:
                        titleTv.setTypeface(Typeface.DEFAULT);
                        titleTv.setText(title);
                }
            }
        }

        void setDescribe(String describe) {
            if (!TextUtils.isEmpty(describe)) {
                describeTv.setText(describe);
            }
        }

    }

}
